package com.example.administrator.fragarmentdemotest_a.fragarment_demo.adapter;

import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.fragarmentdemotest_a.R;
import com.example.administrator.fragarmentdemotest_a.RestScroll.DepthPageTransformer;
import com.example.administrator.fragarmentdemotest_a.RestScroll.RestScroller;
import com.example.administrator.fragarmentdemotest_a.fragarment_demo.Bean.FriendsGroupBean;

import java.lang.reflect.Field;
import java.util.List;

public class RvMsgAdapter extends RecyclerView.Adapter<RvMsgAdapter.BaseMsgViewHolder> {
    List<FriendsGroupBean> grouplist;

    public RvMsgAdapter(List<FriendsGroupBean> grouplist) {
        this.grouplist = grouplist;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @NonNull
    @Override
    public BaseMsgViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        BaseMsgViewHolder viewHolder = null;
        if (viewType == 0) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_vp, parent, false);
            viewHolder = new VpViewHolder(view);
            viewHolder.setIsRecyclable(false);
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_name, parent, false);
            viewHolder = new FriendViewHolder(view);
            viewHolder.setIsRecyclable(false);
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull BaseMsgViewHolder holder, int position) {
        if (holder instanceof VpViewHolder) {
            VpViewHolder viewHolder = (VpViewHolder) holder;
        } else if (holder instanceof FriendViewHolder) {
            FriendViewHolder viewHolder = (FriendViewHolder) holder;
            viewHolder.setData(grouplist.get(position - 1));
        }
    }

    @Override
    public int getItemCount() {
        return grouplist.size() + 1;
    }

    protected class BaseMsgViewHolder extends RecyclerView.ViewHolder {

        public BaseMsgViewHolder(View itemView) {
            super(itemView);
        }
    }

    protected class VpViewHolder extends BaseMsgViewHolder {
        private ViewPager vp;
        private int[] imgs = new int[]{R.mipmap.a, R.mipmap.b, R.mipmap.c, R.mipmap.d, R.mipmap.e};
        private Handler handler = new Handler();

        @SuppressLint("ClickableViewAccessibility")
        public VpViewHolder(View itemView) {
            super(itemView);
            vp = itemView.findViewById(R.id.vp_item_vp);
            A_Messager_Adapter vpAdapter = new A_Messager_Adapter(imgs);
            vp.setAdapter(vpAdapter);
            int middle = vpAdapter.getCount() / 2;
            int start = middle - middle % 4;
            vp.setCurrentItem(start, false);
            vp.setPageTransformer(true, new ViewPager.PageTransformer() {
                @Override
                public void transformPage(@NonNull View page, float position) {
                    raised3D(page, position);
                }
            });
            handler.postDelayed(r, 4000);
            vp.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    switch (motionEvent.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            handler.removeCallbacks(r);
                            break;
                        case MotionEvent.ACTION_UP:
                        case MotionEvent.ACTION_CANCEL:
                            handler.postDelayed(r, 2000);
                            break;
                    }
                    return false;
                }
            });
            RestScroller mScroller = null;
            Field mField = null;
            try {
                mField = ViewPager.class.getDeclaredField("mScroller");
                mField.setAccessible(true);
                mScroller = new RestScroller(vp.getContext());
                mField.set(vp, mScroller);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            //控制速度 秒数*1000=实际切换秒数
            mScroller.setmDuration(3 * 1000);

        }

        private Runnable r = new Runnable() {
            @Override
            public void run() {
                vp.setCurrentItem(vp.getCurrentItem() + 1);
                handler.postDelayed(this, 2000);
            }
        };


    }

    protected class FriendViewHolder extends BaseMsgViewHolder {
        private TextView item_fname;
        private ImageView right_jt;

        public FriendViewHolder(View itemView) {
            super(itemView);
            item_fname = itemView.findViewById(R.id.item_fname);
            right_jt = itemView.findViewById(R.id.right_jt);
        }

        public void setData(final FriendsGroupBean bean) {
            item_fname.setText(bean.getName());
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int flag = 1;
                    LinearLayout ll = view.findViewById(R.id.ll_tree);
                    RecyclerView subView = new RecyclerView(view.getContext());
                    SubViewAdapter adapter = new SubViewAdapter(bean.getFriends());
                    subView.setLayoutManager(new LinearLayoutManager(view.getContext()));
                    subView.setAdapter(adapter);
                    //当flag不为空的时候,获取flag的值。
                    if (ll.getTag() != null) {
                        flag = (int) ll.getTag();
                    }
                    //当flag为1时，添加子布局。否则，移除子布局。
                    if (flag == 1) {
                        ll.addView(subView);
                        subView.setTag(101);
                        ll.setTag(2);
                        ObjectAnimator rotation = ObjectAnimator.ofFloat(right_jt, "rotation", 0.0f, 90f);
                        rotation.setDuration(300);
                        rotation.start();

                    } else {
                        ll.removeView(view.findViewWithTag(101));
                        ll.setTag(1);
                        ObjectAnimator rotation = ObjectAnimator.ofFloat(right_jt, "rotation", 90f, 0f);
                        rotation.setDuration(300);
                        rotation.start();
                    }

                }
            });
        }
    }

    public void raised3D(View view, float position) {
        if (position >= -1 && position <= 1) {
            view.setPivotX(position < 0 ? view.getWidth() : 0);//设置要旋转的Y轴的位置
            view.setRotationY(90 * position);//开始设置属性动画值
        }
    }

    public void sink3D(View view, float position) {
        if (position >= -1 && position <= 1) {
            view.setPivotX(position < 0 ? view.getWidth() : 0);
            view.setRotationY(-90 * position);
        }
    }
}




